# -*- coding: utf-8 -*-
import sqlite3

'''
create table lotto_data(lotto_date text)
show tables
drop table 테이블명
select name from sqlite_master Where type='table' # 모든 테이블명 획득
create table if not exist 테이블명 (lotto_date text)
pragma foreign=on; #설정
pragma foreign; #조회
CREATE TABLE lotto_group(gid integer, name text)
CREATE TABLE lotto_data(val integer, gid integer, FOREIGN KEY(gid) REFERENCES lotto_group(gid))
CREATE TABLE lotto_group(gid integer NOT NULL PRIMARY KEY, name text)
'''

# lotto DB와 연동해주는 역활
# DB에서 데이터를 가져와서 LottoEntry로 저정하거나 LottoEntry를 DB에 저장한다.

class LottoRepository():
    def __init__(self):
        #self.conn = sqlite3.connect(":memory")
        self.conn = None
        
    def init(self):
        self.conn.execute('''create table lotto_data (date text, trans text, symbol text,
 qty real, price real)''')
        
    def connect(self):
        if self.conn != None:
            return 0
        
        self.conn = sqlite3.connect("lotto.data")
        return 1
    
    def close(self):
        if self.conn != None:
            self.conn.close()
            self.conn = None
            
    def getValue(self, groupName, v1):
        return

def createTable(conn):
    return conn.execute("CREATE TABLE lotto_data (name text)")

def existTable(conn, tableName):
    sym = (tableName,)
    value = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", sym)
    ret = value.fetchall()
    return len(ret) != 0
 
def dropTable(conn):
    return conn.execute("DROP TABLE lotto_data")

if __name__ == "__main__":
    conn = sqlite3.connect("lotto.data")
    #c = conn.cursor()
    c = conn
    ret = existTable(c, 'lotto_data')
    print "existTable: ", ret
    ret = createTable(c)
    print "createTable: ", ret
    ret = existTable(c, 'lotto_data')
    print "existTable: ", ret
    ret = dropTable(c)
    print "dropTable: ", ret
    ret = existTable(c, 'lotto_data')
    print "existTable: ", ret
    conn.close()
    
    propFile = file("lotto.properties")
    propDict = dict()
    for propLine in propFile:
        propDef = propLine.strip()
        if 0 == len(propDef):
            continue
        if propDef[0] in ('!', '#'):
            continue
        punctuation= [ propDef.find(c) for c in ':= ' ] + [ len(propDef) ]
        found= min( [ pos for pos in punctuation if pos != -1 ] )
        name= propDef[:found].rstrip()
        value= propDef[found:].lstrip(":= ").rstrip()
        propDict[name]= value
    propFile.close()
    print propDict
    #lottoRepo = LottoRepository()